<!DOCTYPE html>
<html lang="zh-cn">
  <head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta name="author" content="Zhou Wei <zromyk@163.com>">
  <title>【算法】求圆内等概随机点</title>
  <link rel="shortcut icon" href="/favicon.ico">
  <link rel="stylesheet" href="/style/html/pure.css">
  <link rel="stylesheet" href="/style/html/main.css">
  <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/4.7.0/css/font-awesome.css">
  <!-- <link rel="stylesheet" href="https://apps.bdimg.com/libs/highlight.js/9.1.0/styles/default.min.css"> -->
<link rel="stylesheet" href="/style/article/highlight/default.min.css">
<link rel="stylesheet" href="/style/article/pell-1.0.6/dist/pell.css">

</head>
<body>
  <div id="menu-background"></div>
  <div id="menu">
    <div class="pure-menu pure-menu-horizontal">
  <ul class="pure-menu-list block-middle">
    <li class="pure-menu-item">
  <a class="pure-menu-heading" href="/index.html">ZROMYK</a>
</li>
<li class="pure-menu-item">
  <a class="pure-menu-link" href="/index.html">主页</a>
</li>
<li class="pure-menu-item">
  <a class="pure-menu-link" href="/public/archive/index.html">归档</a>
</li>
<li class="pure-menu-item">
  <a class="pure-menu-link" href="/public/download/index.html">下载</a>
</li>
<li class="pure-menu-item">
  <a class="pure-menu-link" href="/public/feedback/index.html">反馈</a>
</li>
<li class="pure-menu-item">
  <a class="pure-menu-link" href="/public/about/index.html">关于我</a>
</li>
<li class="pure-menu-item">
  <a class="pure-menu-link" href="https://github.com/zromyk"><i class="fa fa-github" style="font-size:32px"></i></a>
</li>

  </ul>
</div>

  </div>
  <div id="layout">
    <div class="content">
      <div id="nav">
    <div id="navigation" class="navigation">
  <ul class="pure-menu-list">
    <li class="pure-menu-item">
  <a class="pure-menu-link nav1" onclick="animateByNav()" href="#_1">【算法】求圆内等概随机点</a>
</li>
<li class="pure-menu-item">
  <a class="pure-menu-link nav2" onclick="animateByNav()" href="#2rtheta">方法2：利用极坐标求随机点（<script type="math/tex">r,\theta</script>）（错误，非等概）</a>
</li>
<li class="pure-menu-item">
  <a class="pure-menu-link nav2" onclick="animateByNav()" href="#3sqrtrtheta">方法3：利用极坐标求随机点（<script type="math/tex">\sqrt{r},\theta</script>）</a>
</li>
<li class="pure-menu-item">
  <a class="pure-menu-link nav3" onclick="animateByNav()" href="#_2">连续型随机向量变换的联合分布</a>
</li>
<li class="pure-menu-item">
  <a class="pure-menu-link nav3" onclick="animateByNav()" href="#matlab">在写Matlab代码的时候，由于书写错误，还出现了下面的图形：</a>
</li>

  </ul>
</div>

</div>
<div id="content-articles">
  <h1 id="【算法】求圆内等概随机点" class="content-subhead">【算法】求圆内等概随机点</h1>
  <p>
    <span>2020-10-14</span>
    <span><span class="post-category post-category-algorithm">Algorithm</span></span>
    <button id="button-markdownEditor" class="pure-button" onclick="markdownEditor()">启用编辑</button>
    <button id="button-save" class="pure-button" onclick="save()">保存</button>
  </p>
  <div id="content-articles-markdownEditor" style="display: none;">
    <h1>编辑 power by pell</h1>
    <div id="editor" class="pell"></div>
    <div style="margin-top:20px;">
        <h3>Text output:</h3>
        <div id="text-output"></div>
    </div>
    <div style="margin-top:20px;">
        <h3>HTML output:</h3>
        <pre id="html-output"></pre>
    </div>
  </div>
  <div id="content-articles-markdown">
    <h1 id="_1">【算法】求圆内等概随机点</h1>
<p>要求：给定一个圆心为原点的，半径为1的圆，如何等概地随机取到圆内的一个点？</p>
<h2 id="1xoyxy">方法1：利用xOy坐标求随机点<script type="math/tex">(x,y)</script>
</h2>
<p>描述：随机取xOy坐标下的点<script type="math/tex">(x, y), -1 < x < 1, -1 < y < 1</script>，利用公式 <script type="math/tex">x^2 + y^2 < r^2</script>判断其是否在园内。</p>
<pre><code class="pre-wrap"><span style="overflow-x: auto; max-width:100%; display:inline;"><code class="language-matlab">x1 = rand(5000,1)*2 - 1; 
y1 = rand(5000,1)*2 - 1; 

r2 = 1; 
theta = 0 : pi/100 : 2*pi;
x2 = r2 * cos(theta);
y2 = r2 * sin(theta);

plot(x1,y1,'r.',x2,y2,'-');
</code></span></code></pre>
<p><img class="pure-img" src="https://zromyk.gitee.io/myblog-figurebed/post/【算法】求圆内等概随机点.assets/method1.png" alt="method1" style="zoom:50%;" /></p>
<h2 id="2rtheta">方法2：利用极坐标求随机点（<script type="math/tex">r,\theta</script>）（错误，非等概）</h2>
<blockquote class="content-quote">
<p>本方法并不能等概地取到圆内的一个点。</p>
</blockquote>
<p>描述：随机取极坐标下的点<script type="math/tex">(r,\theta),0 \le r < 1,0 \le \theta < 2\pi</script>。</p>
<pre><code class="pre-wrap"><span style="overflow-x: auto; max-width:100%; display:inline;"><code class="language-matlab">r1 = rand(1000,1);
theta = rand(1000,1);
x1 = r1 .* cos(theta * 2*pi); 
y1 = r1 .* sin(theta * 2*pi);

r2 = 1;
theta = 0 : pi/100 : 2*pi;
x2 = r2 * cos(theta); 
y2 = r2 * sin(theta);

plot(x1,y1,'r.',x2,y2,'-');
</code></span></code></pre>
<p><img class="pure-img" src="https://zromyk.gitee.io/myblog-figurebed/post/【算法】求圆内等概随机点.assets/method2.png" alt="method2" style="zoom:50%;" /></p>
<h2 id="3sqrtrtheta">方法3：利用极坐标求随机点（<script type="math/tex">\sqrt{r},\theta</script>）</h2>
<p>描述：随机取极坐标下的点<script type="math/tex">(\sqrt{r},\theta),0 \le r < 1,0 \le \theta < 2\pi</script>。</p>
<pre><code class="pre-wrap"><span style="overflow-x: auto; max-width:100%; display:inline;"><code class="language-matlab">r1 = rand(5000,1);
theta = rand(5000,1);
x1 = sqrt(r1) .* cos(theta * 2*pi); 
y1 = sqrt(r1) .* sin(theta * 2*pi);

r2 = 1;
theta = 0 : pi/100 : 2*pi;
x2 = r2 * cos(theta); 
y2 = r2 * sin(theta);

plot(x1,y1,'r.',x2,y2,'-');
</code></span></code></pre>
<p><img class="pure-img" src="https://zromyk.gitee.io/myblog-figurebed/post/【算法】求圆内等概随机点.assets/method3.png" alt="method3" style="zoom:50%;" /></p>
<p>该题的证明需要下面的定理：</p>
<h3 id="_2">连续型随机向量变换的联合分布</h3>
<blockquote class="content-quote">
<p>定理：设<script type="math/tex">(X,Y)</script>是联合概率密度为<script type="math/tex">f(x,y)</script>的连续型随机向量，<script type="math/tex">g_1(x,y),g_2(x,y)</script>是两个（分块连续的）二元函数，令<script type="math/tex">\xi=g_1(X,Y),\eta=g_2(X,Y)</script>，如果对任何非负连续的二元函数<script type="math/tex">h(u,v)</script>
</p>
<p>
<script type="math/tex; mode=display">
\int_{-\infty}^\infty\int_{-\infty}^\infty{h[g_1(x,y),g_2(x,y)]}f(x,y)dxdy = \iint_Dh(u,v)p(u,v)dudv
</script>
</p>
<p>成立（其中<script type="math/tex">D</script>为<script type="math/tex">R^2</script>中的某一区域），则<script type="math/tex">(\xi,\eta)</script>的联合概率密度为<br />
<script type="math/tex; mode=display">
f(x,y) = 
\begin{cases}
p(x,y)，(x,y) \subset D \\[2ex]
0，其他
\end{cases}
</script>
</p>
</blockquote>
<p>在本题中，我们令<script type="math/tex">R = r^2</script>，则<script type="math/tex">R</script>是<script type="math/tex">[0,1]</script>上是均匀分布，<script type="math/tex">\theta</script>在<script type="math/tex">[0,2\pi]</script>上是均匀分布，且<script type="math/tex">R,\theta</script>相互独立。</p>
<p>
<script type="math/tex; mode=display">
\begin{equation}\begin{split} 
x = r * \cos\theta &= \sqrt{R} * \cos\theta \\
y = r * \sin\theta &= \sqrt{R} * \sin\theta \\[2em]
设\ g_1(R,\theta) &= \sqrt{R} * \cos\theta \ (为定理中的\xi=g_1(X,Y),(R,\theta为定理中的X,Y)) \\
g_2(R,\theta) &= \sqrt{R} * \sin\theta \ (为定理中的\eta=g_2(X,Y)) \\[2em]
应用上述定理& \iint{h[\sqrt{R} * \cos\theta,\sqrt{R} * \sin\theta]}f(R,\theta)dRd\theta \\
= &\iint{h[r * \cos\theta,r * \sin\theta]}f(r^2,\theta)d(r^2)d\theta \\
= &\iint{h[r * \cos\theta,r * \sin\theta]}f(r^2,\theta)2rdrd\theta \\
= &\iint{h[x,y]}2f(x^2 + y^2,\arctan\cfrac{y}{x})dxdy \\[2em]
其中 dxdy &= d(r\cos\theta)d(r\sin\theta) \\
&= (\cos\theta dr - r\sin\theta d\theta)(\sin\theta dr + r\cos\theta d\theta) \\
&= 0 + r\cos^2\theta drd\theta - r\sin^2\theta d\theta dr + 0\\
&= r\cos^2\theta drd\theta + r\sin^2\theta drd\theta \\
&= r(\cos^2\theta + \sin^2\theta)drd\theta \\
&= rdrd\theta \\[2em]
drdr &= 0 \\
d\theta dr &= -(drd\theta) \\
d\theta d\theta &= 0 \\[2em]
R和\theta均匀分布，且相互独立，&则：f(R) = 1, f(\theta) = \cfrac{1}{2\pi} \\
f(R,\theta) &= f(R) * f(\theta) = \cfrac{1}{2\pi} \\
(\xi,\eta)的联合概率密度为：&2f(x^2 + y^2,\arctan\cfrac{y}{x}) \\ 
& = 2 * f(r^2,\theta) = 2 * f(R,\theta) \\
& = 2 * \cfrac{1}{2\pi} = \cfrac{1}{\pi} \\[2em]
&即(x,y)或(g_1,g_2)的联合概率密度为：\cfrac{1}{\pi} \\
这表示(x,y)&在(0 \le r < 1,0 \le \theta < 2\pi)表示的圆内为均匀分布
\end{split}\end{equation}
</script>
</p>
<h3 id="matlab">在写Matlab代码的时候，由于书写错误，还出现了下面的图形：</h3>
<blockquote class="content-quote">
<p>费马螺线：等角螺线的一种，表达式为<script type="math/tex">r = \sqrt{\theta}</script>
</p>
</blockquote>
<pre><code class="pre-wrap"><span style="overflow-x: auto; max-width:100%; display:inline;"><code class="language-matlab">A = rand(5000,1);
x1 = sqrt(A) .* cos(A * 2*pi); 
y1 = sqrt(A) .* sin(A * 2*pi);

r2 = 1;
theta = 0 : pi/100 : 2*pi;
x2 = r2 * cos(theta); 
y2 = r2 * sin(theta);

plot(x1,y1,'r.',x2,y2,'-');
</code></span></code></pre>
<p><img class="pure-img" src="https://zromyk.gitee.io/myblog-figurebed/post/【算法】求圆内等概随机点.assets/accident.png" alt="accident" style="zoom:50%;" /></p>
  </div>
</div>
 
    </div>
  </div>
  <div id="footer-background">
    <div id="footer">
      <div class="legal pure-g">
  <div class="pure-u-1 u-sm-1-2">
    <p class="legal-license"><a href="https://beian.miit.gov.cn/#/Integrated/index">浙ICP备2020038748号</a></p>
  </div>
  <div class="pure-u-1 u-sm-1-2">
    <p class="legal-links"><a href="https://github.com/zromyk">GitHub</a></p>
    <p class="legal-copyright">Copyright © 2021 Wei Zhou. 保留所有权利。</p>
  </div>
</div>
    </div>
  </div>
  <!-- <script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script> -->
  <script src="/style/html/jquery.min.js"></script>
  <script src='/style/article/latex/latest.js?config=TeX-MML-AM_CHTML'></script>
<!-- <script src="https://cdn.geogebra.org/apps/deployggb.js"></script> -->
<script src="/style/article/deployggb.js"></script>
<!-- <script src="https://apps.bdimg.com/libs/highlight.js/9.1.0/highlight.min.js"></script> -->
<script type="text/javascript">
  // 脚本：navigation 随鼠标移动自动变换宽度
  var element = document.getElementById("navigation"); // 获取要操作的元素
  var elementWidth = parseInt(getComputedStyle(element).width);
  var elementLeft = 0;
  var elementRight = 0;
  element.addEventListener('mouseenter', function (event) { // 添加鼠标按下事件的监听器
    elementLeft = element.getBoundingClientRect().left - 10;
    elementRight = element.getBoundingClientRect().left + elementWidth * 3;
    window.addEventListener('mousemove', resize); // 添加全局的鼠标移动事件的监听器
  });

  function resize(event) {
    var minWidth = elementWidth;
    var maxWidth = elementWidth * 2.5;
    // console.log(elementLeft, event.clientX, elementRight, event.clientX - elementLeft + elementWidth / 2);
    if (elementLeft <= event.clientX && event.clientX <= elementRight) {
      var width = event.clientX - elementLeft + elementWidth / 2;
      width = Math.min(width, maxWidth);
      width = Math.max(width, minWidth);
      element.style.width = width + 'px'; // 设置新的宽度样式属性
    }
    else {
      element.style.width = elementWidth + 'px'; // 设置新的宽度样式属性
      stopResize();
    }
  }

  function stopResize() {
    element.style.width = elementWidth + 'px'; // 设置新的宽度样式属性
    // console.log("stopResize", elementLeft, event.clientX, elementRight, event.clientX - elementLeft + elementWidth / 2);
    window.removeEventListener('mousemove', resize); // 移除鼠标移动事件的监听器
  }
</script>
<script src="/style/article/highlight/highlight.min.js"></script>
<script type="text/javascript">
  // 脚本：code语法高亮
  hljs.initHighlightingOnLoad();
</script>
<script>
  function animateByNav() {
    $("html").animate({
        scrollTop: ($(event.target.hash).offset().top - 52)
    }, 300);
  };
</script>
<script src="/style/article/pell-1.0.6/dist/pell.js"></script>
<script>
  // 脚本：自由编辑页面
  var editor = window.pell.init({
    element: document.getElementById('editor'),
    defaultParagraphSeparator: 'p',
    onChange: function(html) {
        document.getElementById('text-output').innerHTML = html
        document.getElementById('html-output').textContent = html
    }
  });

  function markdownEditor() {
    var articles = document.getElementById('content-articles-markdown');
    if (articles.getAttribute("contenteditable") == "true") {
        articles.setAttribute("contenteditable", "false");
        document.getElementById("content-articles-markdownEditor").style.display = "none"; //隐藏
        document.getElementById("button-markdownEditor").innerHTML = "启用编辑";
    } else {
        articles.setAttribute("contenteditable", "true");
        document.getElementById("content-articles-markdownEditor").style.display = ""; //显示
        document.getElementById("button-markdownEditor").innerHTML = "关闭编辑";
    }
  };

  function save() {
      window.alert("保存成功");
  };
</script>

</body>
</html>
